package cn.dly.login.controller;


import cn.dly.login.entity.User;
import cn.dly.login.exception.LRException;
import cn.dly.login.service.UserService;
import cn.dly.login.utils.Container;
import cn.dly.login.utils.JwtUtils;
import cn.dly.login.utils.R;
import cn.dly.login.vo.InfoVo;
import cn.dly.login.vo.LoginVo;
import cn.dly.login.vo.MsgVo;
import cn.dly.login.vo.RegisterVo;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RequestMapping("/sys/lr")
@RestController
@Api(tags = "登录注册")
public class LRController {

    @Autowired
    private UserService userService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation("登录")
    @PostMapping("login")
    public R login(LoginVo loginVo) {
        String token = userService.login(loginVo);
        return R.ok().data("token", token);
    }

    @ApiOperation("注册")
    @PostMapping("register")
    public R register(RegisterVo registerVo) {
        userService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("auth/getLoginInfo")
    public R getLoginInfo(HttpServletRequest request) {
        try {
            String username = JwtUtils.getUserUNByJwtToken(request);
            LoginVo loginVo = userService.getLoginVo(username);
            return R.ok().data("loginVo", loginVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LRException(20001, "error");
        }
    }

    @ApiOperation("验证username是否已存在")
    @PostMapping("yzun")
    public R getyzu(RegisterVo registerVo) {
        String username=registerVo.getUsername();
        Integer count = userService.getyzu(username);
        if (count != 0) {
            return R.ok();
        } else {
            throw new LRException(20001, "error");
        }
    }

    @ApiOperation("验证mobile是否已存在")
    @PostMapping("yzum")
    public R getyzm(RegisterVo registerVo) {
        String mobile=registerVo.getMobile();
        Integer count = userService.getyzm(mobile);
        if (count != 0) {
            return R.ok();
        } else {
            throw new LRException(20001, "error");
        }
    }

    @ApiOperation("修改密码时获取用户名")
    @PostMapping("obtainName")
    public R obtainName(RegisterVo registerVo) {
        String username=registerVo.getUsername();
        if (username != null) {
            Container.list.add(username);
            return R.ok();
        } else {
            throw new LRException(20001, "error");
        }
    }

    @ApiOperation("修改密码")
    @PostMapping("modify")
    public R modify(LoginVo loginVo) {
        int count = userService.modify(loginVo.getPassword(), Container.list.get(Container.list.size() - 1));
        if (Container.list.size() > 5) {
            String name = Container.list.get(Container.list.size() - 1);
            Container.list.clear();
            Container.list.add(name);
        }
        if (count == 1) {
            return R.ok();
        } else {
            throw new LRException(20001, "error");
        }
    }

    @ApiOperation("获取短信验证码")
    @PostMapping("getmsg")
    public R getmsg(MsgVo msgVo) {
        String mobleCode=redisTemplate.opsForValue().get(msgVo.getMobile());
        if(mobleCode!=null){
            return R.ok().data("msg",mobleCode);
        }
        else{
            return R.error();
        }
    }

}
